package com.fzc.rhinooffice.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.webkit.WebView;

import com.fzc.rhinooffice.R;

import org.apache.commons.lang.StringEscapeUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by cong on 15/5/20.
 */
public class CommonUtil {

	public static String getMD5(String val) throws NoSuchAlgorithmException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(val.getBytes());
		byte[] m = md5.digest();// 加密
		return getString(m);
	}

	private static String getString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			sb.append(b[i]);
		}
		return sb.toString();
	}

	public static void loadDataWithTransparency(WebView webView, String content) {

		webView.setBackgroundColor(0);
		webView.setBackgroundResource(R.color.transparent);
		webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // 取消滚动条白边效果
		webView.setHorizontalScrollBarEnabled(false);
		webView.getSettings().setJavaScriptEnabled(true);

		String linkCss = "<link rel=\"stylesheet\" href=\"file:///android_asset/cleanStyle.css\" type=\"text/css\">";

		String js_cleanBgColor = "<script language=javascript>\n"
				+ "function dropTrBgColor()\n" + "{\n"
				+ " var trs = document.getElementsByTagName(\"SPAN\") ;\n"
				+ "    for(var i=0;i<trs.length;i++)\n" + "    {\n"
				+ "        trs[i].style.backgroundColor=\"\";\n" + "    }\n"
				+ "}\n" + "</script>";

		String head = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n"
				+ "<meta charset=\"utf-8\">\n" + js_cleanBgColor
				+ "<title></title>\n" + "</head>\n"
				+ "<body onload=\"dropTrBgColor()\">";

		String bottom = "</body>\n" + "</html>";

		content = head + StringEscapeUtils.unescapeHtml(content) + bottom;

		webView.loadData(content, "text/html; charset=UTF-8", null);

	}

	public static boolean isNetworkConnected(Context context) {
		
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}
	
	
}
